package com.hualan.service;

import com.alibaba.fastjson.JSON;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;
import com.hualan.entity.XiaoQu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/add","/delete","/update","/find"})
public class AreaService extends HttpServlet {
    private List<XiaoQu> xiaoquses;
    @Override
    public void init() throws ServletException {
        this.xiaoquses = new ArrayList<>();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.xiaoquses.clear();
        //获取请求网址
        if(!req.getServletPath().equals("/find")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        //定义sql语句完成数据库指定的操作
        //?是一个占位符，代表当前位置存在一个外界的变量数据
        List<Object> params = new ArrayList<>();
        String sql;
        if (req.getParameter("communityName")!=null&&req.getParameter("createTime")!=null){
            sql = "select * from xiaoqu where communityName=? and createTime=?";
            params.add(req.getParameter("communityName"));
            params.add(req.getParameter("createTime"));
        } else if (req.getParameter("createTime")==null&&req.getParameter("communityName")!=null) {
            sql = "select * from xiaoqu where communityName=?";
            params.add(req.getParameter("communityName"));
        }else if (req.getParameter("communityName")==null&&req.getParameter("createTime")!=null) {
            sql = "select * from xiaoqu where createTime=?";
            params.add(req.getParameter("createTime"));
        }else{
            sql = "select * from xiaoqu";
        }

        try {
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            //4.提取结果集中的数据
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory(XiaoQu.class);
                XiaoQu xiaoQu = (XiaoQu) entityFactory.startCombine(resultSet);
                this.xiaoquses.add(xiaoQu);
            }
            resp.getWriter().write(JSON.toJSONString(this.xiaoquses));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getServletPath().equals("/add")){
            List<Object> params = new ArrayList<>();
            String sql = "insert into xiaoqu(communityName,communityAddress,createTime,communityProvince,communityCity,communityDistrict,communityLatitude,communityLongitude) values(?,?,?,?,?,?,?,?)";
            params.add(req.getParameter("communityName"));
            params.add(req.getParameter("communityAddress"));
            params.add(req.getParameter("createTime"));
            params.add(req.getParameter("communityProvince"));
            params.add(req.getParameter("communityCity"));
            params.add(req.getParameter("communityDistrict"));
            params.add(req.getParameter("communityLatitude"));
            params.add(req.getParameter("communityLongitude"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
            resp.getWriter().write(params.toString());
            resp.getWriter().write("添加成功");

        } else if (req.getServletPath().equals("/delete")) {
            String sql = "delete from xiaoqu where communityId=?";
            List<Object> params = new ArrayList<>();
            params.add(req.getParameter("communityId"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
        } else if (req.getServletPath().equals("/update")) {
            List<Object> params = new ArrayList<>();
            String sql = "update xiaoqu set communityName=?,communityAddress=?,createTime=?,communityProvince=?,communityCity=?,communityDistrict=?,communityLatitude=?,communityLongitude=? where communityId=?";
            params.add(req.getParameter("communityName"));
            params.add(req.getParameter("communityAddress"));
            params.add(req.getParameter("createTime"));
            params.add(req.getParameter("communityProvince"));
            params.add(req.getParameter("communityCity"));
            params.add(req.getParameter("communityDistrict"));
            params.add(req.getParameter("communityLatitude"));
            params.add(req.getParameter("communityLongitude"));
            params.add(req.getParameter("communityId"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
            resp.getWriter().write(params.toString());
            resp.getWriter().write("修改成功");

        }
    }
}
